import { ref, getCurrentInstance } from 'vue';

export function useSystemTime() {
    const instance = getCurrentInstance();//获取当前组件实例
    const systemTime = ref(getSystemTime());//初始化为当前时间
    //获取系统时间
    function getSystemTime() {
        const now = new Date();
        const year = now.getFullYear();
        const month = ('0' + (now.getMonth() + 1)).slice(-2); // 月份从0开始计数，所以需加1 月份转成两位数
        const day = ('0' + (now.getDate())).slice(-2);
        const hour = now.getHours();
        const minute = now.getMinutes();
        const second = now.getSeconds();
        return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    }
    if (instance) {
        //systemTime 变量添加到全局属性中，确保在模板中可以直接使用
        instance.appContext.config.globalProperties.systemTime = systemTime;
    }
    return {
        systemTime
    };
}
